package HighLevelFunction

object Map_Function {
  def main(args: Array[String]): Unit = {
    val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
    //过滤，选取偶数
    val evenList = list.filter(
      _ % 2 == 0
    )
    println(evenList)
    //选择奇数
    println(list.filter(
      (elem: Int) => {
        elem % 2 == 1
      }
    ))
    println("-" * 10)
    //map转换或映射操作
    val map1 = list.map((elem: Int) => {
      elem * elem
    })
    println(map1)
    println(list.map(x => x * 2))
    println("*" * 20)
    //扁平化操作
    val list1 = List(List(1, 2, 3), List(4, 5), List(6, 7, 8, 9))
    val flatList: List[Int] = list1(0) ::: list1(1) ::: list1(2)
    println(flatList)
    val flatten: List[Int] = list1.flatten
    println(flatten)
    println("===============================")
    //扁平映射：将一组字符串进行分词，并保存成单词的列表
    //以空格为分割，分开之后直接做打散操作
    val strings: List[String] = List("hello word", "hello scala", "hello java", "we study hard")
    val stringses: List[Array[String]] = strings.map(elem => elem.split(" "))
    val flatten1: List[String] = stringses.flatten
    println(flatten1)
    val strings1: List[String] = strings.flatMap(elem => elem.split(" "))
    println(strings1)
    println("分组处理：")
    //把list分成奇数偶数
    val booleanToInts: Map[Boolean, List[Int]] = flatList.groupBy(elem => elem % 2 == 0)
    println(booleanToInts)
    val stringToInts: Map[String, List[Int]] = flatList.groupBy(data => if (data % 2 == 0) "偶数" else "奇数")
    println(stringToInts)
    //给定一组词汇，按照单词的首字母进行分组
    val wordlist=List("china","america","canada","cary","bob","japan")
    println(wordlist.groupBy(_.charAt(0)))
  }
}
